package com.leetcode.ask;

public class Number42 {

    public static int trap(int[] height) {
        int area = 0;
        if (height.length <= 2) {
            return 0;
        }
        int l = 0, r = height.length - 1;
        int maxL = height[l], maxR = height[r];
        while (l <= r) {
            if (maxL <= maxR) {
                int c = maxL - height[l];
                if (c == 0) {
                    l++;
                } else if (c < 0) {
                    maxL = height[l];
                    l++;
                } else {
                    area += c;
                    l++;
                }
            } else {
                int c = maxR - height[r];
                if (c == 0) {
                    r--;
                } else if (c < 0) {
                    maxR = height[r];
                    r--;
                } else {
                    area += c;
                    r--;
                }
            }


        }
        return area;
    }

    public static void main(String[] args) {
        int height[] = new int[]{5, 5, 1, 7, 1, 1, 5, 2, 7, 6};
        System.out.println(trap(height));
    }
}
